package be.novelfaces.component.util.attribute;

import java.util.Arrays;

/**
 * 
 * A "pass through" attribute is an HTML attributes that does not change on page
 * translation. The raw name and value of the attribute is just rendered as is.
 * 
 */
public class PassThroughAttribute implements Comparable<PassThroughAttribute> {

	public enum Kind {
		BOOLEAN, GENERIC, URI
	}

	private final String name;
	private final String property;
	private String[] events = {};
	private Kind kind = Kind.GENERIC;
	
	private PassThroughAttribute(String name, String property) {
		this.name = name;
		this.property = property;
	}

	public static PassThroughAttribute attr(AttributeType attributeType) {
		return new PassThroughAttribute(attributeType.getName(),
				attributeType.getProperty()).setEvents(attributeType
				.getEvents());
	}

	public static PassThroughAttribute attr(String name) {
		return new PassThroughAttribute(name, name);
	}

	public static PassThroughAttribute attr(String name, String property) {
		return new PassThroughAttribute(name, property);
	}

	public PassThroughAttribute setEvents(String... events) {
		this.events = events;
		return this;
	}

	public PassThroughAttribute setKind(Kind kind) {
		this.kind = kind;
		return this;
	}

	public String getName() {
		return name;
	}

	public String[] getEvents() {
		return events;
	}

	public String getProperty() {
		return property;
	}

	public Kind getKind() {
		return kind;
	}

	public int compareTo(PassThroughAttribute o) {
		return this.getName().compareTo(o.getName());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((property == null) ? 0 : property.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PassThroughAttribute other = (PassThroughAttribute) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (property == null) {
			if (other.property != null)
				return false;
		} else if (!property.equals(other.property))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "PassThroughAttribute [name=" + name + ", property=" + property
				+ ", events=" + Arrays.toString(events) + ", kind=" + kind
				+ "]";
	}

}
